package sk.tuke.constraintbuilder;

import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class CBPreferencePage extends PreferencePage implements	IWorkbenchPreferencePage {
	
	private Boolean autoAnalyzeEnabled;
	private Button autoAnalyzeEnabledBtn;

	public CBPreferencePage() {
	}

	public CBPreferencePage(String title) {
		super(title);
	}

	public CBPreferencePage(String title, ImageDescriptor image) {
		super(title, image);
	}

	@Override
	public void init(IWorkbench workbench) {
		IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode("sk.tuke.constraintbuilder");
		autoAnalyzeEnabled = Boolean.valueOf(preferences.get("autoAnalyzeEnabled", "true"));
		
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		autoAnalyzeEnabledBtn = new Button(composite, SWT.CHECK);
		autoAnalyzeEnabledBtn.setText("Enable auto analyze on startup");
		autoAnalyzeEnabledBtn.setSelection(autoAnalyzeEnabled);
		return composite;
	}
	
	@Override
	public boolean performOk() {
		IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode("sk.tuke.constraintbuilder");
		preferences.put("autoAnalyzeEnabled", String.valueOf(autoAnalyzeEnabledBtn.getSelection()));
		try {
			preferences.flush();
		} catch (org.osgi.service.prefs.BackingStoreException e) {
		}
		return super.performOk();
	}

}
